local Wqc = {}

-- 角色向公屏发送测试消息
---@param player ServerPlayer @ 要发消息的角色
---@param text string @ 要发送的消息
Wqc.speak = function(player, text)
  local msg = {}
  msg.type = "#speakMsg"
  msg.to = { player.id }
  msg.arg = text
  player.room:sendLog(msg)
end
Fk:loadTranslationTable{
  ["#speakMsg"] = "%to：%arg",
}

-- 在新的一行输出翻译表中的中文翻译
---@param src string @ 翻译表中待翻译的文本
---@param lang? string @ 要使用的语言
---@return string @ 另起一行的翻译后的文本
Wqc.getTranslateInNewLine = function(src, lang)
  return "<br>"..Fk:translate(src, lang)
end

-- 获取角色对应Mark并初始化为table
---@param player Player @ 要被获取标记的那个玩家
---@param mark string @ 标记
---@return table
Wqc.getMarkOrInitTable = function(player, mark)
  return type(player:getMark(mark)) == "table" and player:getMark(mark) or {}
end

-- 统计角色武将牌上牌的数量（即所有特殊牌堆的牌数）
---@param player Player @ 需要统计哪名角色
---@return number @ 牌数
Wqc.getAllPileSize = function (player)
  local num = 0
  for _, v in pairs(player.special_cards) do
    num = num + #v
  end
  return num
end

-- 恐惧标记的效果
local wq_common_mark_kongju_prohibit = fk.CreateProhibitSkill{
  name = "#wq_common_mark_kongju_prohibit",
  global = true,
  prohibit_use = function(self, player, card)
    return player:getMark("@wq_mark_kongju") ~= 0 and card.type == Card.TypeBasic
  end,
  prohibit_response = function(self, player, card)
    return player:getMark("@wq_mark_kongju") ~= 0 and card.type == Card.TypeBasic
  end,
}
local wq_common_mark_kongju_clear = fk.CreateTriggerSkill{
  name = "#wq_common_mark_kongju_clear",
  refresh_events = {fk.EventPhaseStart},
  global = true,
  can_refresh = function(self, event, target, player, data)
    return target == player and player.phase == Player.Start and player:getMark("@wq_mark_kongju") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@wq_mark_kongju", 0)
  end,
}
wq_common_mark_kongju_prohibit:addRelatedSkill(wq_common_mark_kongju_clear)
Fk:addSkill(wq_common_mark_kongju_prohibit)
Fk:loadTranslationTable{
  ["#wq_common_mark_kongju_prohibit"] = "恐惧",
  ["#wq_common_mark_kongju_description"] = "<font color='grey'><b>「恐惧」标记</b><br>拥有「恐惧」标记的角色无法使用或打出基本牌，该角色的准备阶段移除其所有「恐惧」标记。",
}

-- 感电标记的效果
local wq_common_mark_gandian_damage = fk.CreateTriggerSkill{
  name = "#wq_common_mark_gandian_damage",
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  global = true,
  refresh_events = {fk.Damaged},
  can_refresh = function(self, event, target, player, data)
    return target == player and data.card and data.card.type == Card.TypeTrick and player:getMark("@wq_mark_gandian") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    Wqc.doGandianJudge(player)
  end,
}
-- 感电判定
Wqc.doGandianJudge = function(player)
  local room = player.room
  local num = math.min(player:getMark("@wq_mark_gandian"), 13)
  if num <= 0 then return end
  local judge = {
    who = player,
    reason = "#wq_common_mark_gandian_damage",
    pattern = ".|1~"..num.."|spade",
  }
  room:judge(judge)
  local result = judge.card
  if result.suit == Card.Spade and result.number >= 1 and result.number <= num then
    room:damage{
      to = player,
      damage = 2,
      damageType = fk.ThunderDamage,
      skillName = "#wq_common_mark_gandian_damage",
    }
    if player:isAlive() then
      room:setPlayerMark(player, "@wq_mark_gandian", 0)
    end
  end
end
Fk:addSkill(wq_common_mark_gandian_damage)
Fk:loadTranslationTable{
  ["#wq_common_mark_gandian_damage"] = "感电",
  ["#wq_common_mark_gandian_description"] = "<font color='grey'><b>「感电」标记</b><br>拥有「感电」标记的角色受到锦囊牌造成的伤害后需进行“感电判定”：若判定牌的花色为黑桃且点数不大于其拥有的「感电」标记数，其受到2点雷电伤害，并移除所有「感电」标记。",
}

-- 流血标记的效果
local wq_common_mark_liuxue_loseHp = fk.CreateTriggerSkill{
  name = "#wq_common_mark_liuxue_loseHp",
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  global = true,
  refresh_events = {fk.TurnStart},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("@wq_mark_liuxue") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    Wqc.doLiuxueJudge(player)
  end,
}
-- 流血判定
Wqc.doLiuxueJudge = function(player)
  local room = player.room
  local num = math.min(player:getMark("@wq_mark_liuxue"), 13)
  if num <= 0 then return end
  local judge = {
    who = player,
    reason = "#wq_common_mark_liuxue_loseHp",
    pattern = ".|1~"..num.."|.",
  }
  room:judge(judge)
  local result = judge.card
  if result.number >= 1 and result.number <= num then
    room:loseHp(player, 1, "#wq_common_mark_liuxue_loseHp")
  end
end
Fk:addSkill(wq_common_mark_liuxue_loseHp)
Fk:loadTranslationTable{
  ["#wq_common_mark_liuxue_loseHp"] = "流血",
  ["#wq_common_mark_liuxue_description"] = "<font color='grey'><b>「流血」标记</b><br>拥有「流血」标记的角色需在回合开始时进行“流血判定”：若判定牌的点数不大于其拥有的「流血」标记数，其失去1点体力。",
}

-- 燃烧标记的效果
local wq_common_mark_ranshao_damage = fk.CreateTriggerSkill{
  name = "#wq_common_mark_ranshao_damage",
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  global = true,
  refresh_events = {fk.TurnStart},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("@wq_mark_ranshao") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    Wqc.doRanshaoJudge(player)
  end,
}
-- 燃烧判定
Wqc.doRanshaoJudge = function(player)
  local room = player.room
  local num = math.min(player:getMark("@wq_mark_ranshao"), 13)
  if num <= 0 then return end
  local judge = {
    who = player,
    reason = "#wq_common_mark_ranshao_damage",
    pattern = ".|1~"..num.."|.",
  }
  room:judge(judge)
  local result = judge.card
  if result.number >= 1 and result.number <= num then
    room:damage{
      to = player,
      damage = 1,
      damageType = fk.FireDamage,
      skillName = "#wq_common_mark_ranshao_damage",
    }
  end
end
Fk:addSkill(wq_common_mark_ranshao_damage)
Fk:loadTranslationTable{
  ["#wq_common_mark_ranshao_damage"] = "燃烧",
  ["#wq_common_mark_ranshao_description"] = "<font color='grey'><b>「燃烧」标记</b><br>拥有「燃烧」标记的角色需在回合开始时进行“燃烧判定”：若判定牌的点数不大于其拥有的「燃烧」标记数，其受到1点火焰伤害。",
}

-- 破绽标记的效果
local wq_common_mark_pozhan_prohibit = fk.CreateProhibitSkill{
  name = "#wq_common_mark_pozhan_prohibit",
  global = true,
  is_prohibited = function(self, from, to, card)
    local targets = table.filter(Fk:currentRoom().alive_players, function(p)
      return p:getMark("@wq_mark_pozhan") > 0
    end)
    if #targets > 0 then
      return card.trueName == "slash" and not table.contains(targets, to)
    end
  end,
}
local wq_common_mark_pozhan_clear = fk.CreateTriggerSkill{
  name = "#wq_common_mark_pozhan_clear",
  refresh_events = {fk.EventPhaseStart, fk.TargetConfirmed},
  global = true,
  can_refresh = function(self, event, target, player, data)
    if target ~= player or player:getMark("@wq_mark_pozhan") <= 0 then return false end
    if event == fk.EventPhaseStart then
      return player.phase == Player.Start
    elseif event == fk.TargetConfirmed then
      return data.card.trueName == "slash"
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:removePlayerMark(player, "@wq_mark_pozhan", 1)
  end,
}
wq_common_mark_pozhan_prohibit:addRelatedSkill(wq_common_mark_pozhan_clear)
Fk:addSkill(wq_common_mark_pozhan_prohibit)
Fk:loadTranslationTable{
  ["#wq_common_mark_pozhan_prohibit"] = "破绽",
  ["#wq_common_mark_pozhan_description"] = "<font color='grey'><b>「破绽」标记</b><br>当场上存在「破绽」标记时，每名角色使用【杀】只能指定有「破绽」标记的角色为目标。拥有「破绽」标记的角色的准备阶段或当其成为【杀】的目标后，其需移除1枚「破绽」标记。",
}

-- 负面标记
Wqc.NEGATIVE_MARKS = {"@wq_mark_kongju", "@wq_mark_gandian", "@wq_mark_liuxue", "@wq_mark_ranshao", "@wq_mark_pozhan"}
-- 判断角色是否拥有负面标记
---@param player ServerPlayer @ 待判断的角色
---@return bool @ 是否拥有负面标记
Wqc.hasNagtiveMarks = function(player)
  for _, mark in pairs(Wqc.NEGATIVE_MARKS) do
    if player:getMark(mark) > 0 then
      return true
    end
  end
  return false
end
-- 清除角色的负面标记
---@param player ServerPlayer @ 待清除标记的角色
Wqc.clearNagtiveMarks = function(player)
  local room = player.room
  for _, mark in pairs(Wqc.NEGATIVE_MARKS) do
    room:setPlayerMark(player, mark, 0)
  end
end
Fk:loadTranslationTable{
  ["@wq_mark_kongju"] = "恐惧",
  ["@wq_mark_gandian"] = "感电",
  ["@wq_mark_liuxue"] = "流血",
  ["@wq_mark_ranshao"] = "燃烧",
  ["@wq_mark_pozhan"] = "破绽",
  ["#wq_common_mark_negative_description"] = "<font color='grey'><b>负面标记</b><br>对「恐惧」、「感电」、「流血」、「燃烧」、「破绽」标记的统称。",
}

return Wqc